package com.yundasys.lab02springbootstudy.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @PACKAGE_NAME: com.yundasys.lab02springbootstudy.future
 * @NAME: CompletableFuture3
 * @USER: jiangta
 * @DATE: 2021/12/9
 * @TIME: 8:08 上午
 * @PROJECT_NAME: springboot-labs
 */
public class CompletableFuture4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //可以获取到返回值，可传入自定义线程池
        //public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
        //public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
        //没有返回值，可传入自定义线程池
        //public static CompletableFuture<Void> runAsync(Runnable runnable)
        //public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor)

        ExecutorService service = Executors.newFixedThreadPool(10);
        CompletableFuture.runAsync(()->{
            System.out.println("异步任务成功完成了");
        }, service);

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            return "返回值";
        }, service);
        System.out.println(future.get());


        //上一个任务完成和上一个任务用同一个线程
        //public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
        //交给线程池重新启动一个线程
        //public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);
        //任务执行完成后（只能感知）
        //public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
        //感知异常同时修改返回值
        //public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn);

    }
}
